﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using nplJobsCommon.Common;
using nplJobs.DB;
using System.Data;
using nplJobsCommon.JobSeeker;
using System.Data.SqlClient;

namespace nplJobsDAL.Common
{

    /// <summary>
    /// Select professional info from the database
    /// </summary>
    public class SelectProfessionalInformation : DataAccessBase
    {

        public Int64 ProfessionalInfoID { get; set; }
        public Guid JobseekerID { get; set; }


        public SelectProfessionalInformation()
        {
            StoredProcedureName = StoredProcedure.Name.spSelectProfessionalInfoByID.ToString();
        }

        #region GetProfessionalInfo
        public List<ProfessionalInfo> GetProfessionalInfo()
        {
            DataBaseHelper dbHelper;
            DataSet dsProfessionalInfo;

            try
            {
                SelectProfessionalInfoParameters selectProfessionalInfoParameters = new SelectProfessionalInfoParameters(this.ProfessionalInfoID, this.JobseekerID);
                dbHelper = new DataBaseHelper(StoredProcedureName);
                dbHelper.Parameters = selectProfessionalInfoParameters.Parameters;
                dsProfessionalInfo = dbHelper.Run(base.ConnectionString, dbHelper.Parameters);
                return GetList(dsProfessionalInfo);
            }
            catch (Exception)
            {

                throw;
            }
        }
        #endregion

        #region SQL parameters for selecting professional info by id
        public class SelectProfessionalInfoParameters
        {
            private SqlParameter[] _parameters;

            public SelectProfessionalInfoParameters(Int64 professionalInfoID,Guid JobseekerId)
            {
                this.ProfessionalInfoID = professionalInfoID;
                this.JobseekerID = JobseekerId;
                Build();
            }


            private void Build()
            {
                SqlParameter[] parameters = 
                {
                    new SqlParameter("@ProfessionalInfoID", ProfessionalInfoID),
                    new SqlParameter("@JobseekerID", JobseekerID),

                };

                Parameters = parameters;
            }

            public Int64 ProfessionalInfoID { get; set; }
            public Guid JobseekerID { get; set; }


            public SqlParameter[] Parameters
            {
                get { return _parameters; }
                set { _parameters = value; }
            }
        }
        #endregion

        #region GetList
        private List<ProfessionalInfo> GetList(DataSet dsProfessionalInfo)
        {
            List<ProfessionalInfo> professionalinfo;
            try
            {
                professionalinfo = (from n in dsProfessionalInfo.Tables[0].AsEnumerable()
                                    select new ProfessionalInfo
                                    {
                                        JobSeekerID = n.Field<Guid>("JobSeekerID"),

                                        ProfessionalInfoID = n.Field<Int64>("ProfessionalInfoID"),
                                        CompanyName = n.Field<string>("CompanyName"),
                                        IndustryID = n.Field<int>("IndustryID"),
                                        Title = n.Field<string>("Title"),
                                        JobTypeID = n.Field<int>("JobTypeID"),
                                        CareerLevelID = n.Field<int>("CareerLevelID"),
                                        EmployeeRangeID = n.Field<int>("EmployeeRangeID"),
                                        OrganizationTypeID = n.Field<int>("OrganizationTypeID"),
                                        CompanyHomePage = n.Field<string>("CompanyHomePage"),
                                        CurrentPosition = n.Field<bool>("CurrentPosition"),
                                        PositionDescription = n.Field<string>("PositionDescription"),
                                        DisplayAtTop = n.Field<bool?>("DisplayAtTop"),
                                        Address = n.Field<string>("Address"),
                                        FromDate = n.Field<DateTime>("FromDate"),
                                        ToDate = n.Field<DateTime>("ToDate"),

                                    }).ToList();

                    return professionalinfo;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
            finally { }
        }
        #endregion
    }
}

